<template>
  <div class="roles_content users_content">
    <div class="card-container">
    <div class="header">
      <el-form
        class="demo-form-inline"
        size="mini"
        label-position="right"
        label-width="100px"
        ref="queryRoleRef"
        :rules="rules"
        :model="formOptions"
      >
        <div class="row">
          <div class="col">
            <el-form-item label="地域名称" prop="name">
              <el-input
                v-model="formOptions.name"
                placeholder="请输入地域名称"
              ></el-input>
            </el-form-item>
          </div>
          <div style="width:250px"
          >
              <el-button class="header-btn" type="primary" size="mini" @click="resetForm('queryRoleRef')"
                >
                <img src="../../../assets/img/globalTable/icon2-reset.png" alt="">
                重置</el-button
              >
              <el-button class="header-btn" type="primary" size="mini" @click="queryRolesclick"
                >
                <img src="../../../assets/img/globalTable/icon1-search.png" alt="">
                查询</el-button
              >
              <el-button class="header-btn" type="primary" size="mini" @click="addClick">
                <img src="../../../assets/img/globalTable/icon3-add.png" alt="">
                新增</el-button>
          </div>
        </div>
      </el-form>
    </div>

    <el-card >
      <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        stripe
        size="mini"
        :height="tableHeight"
      >
        <el-table-column
          label="序号"
          type="index"
          :index="indexMethod"
        ></el-table-column>
        <el-table-column
          label="地域名称"
          prop="name"
        ></el-table-column>

        <el-table-column label="操作">
          <template slot-scope="{ row, $index }">
            <span class="operate-btn" @click="lookClick(row)" type="primary" size="mini"
              >查看</span
            >
            <span class="operate-btn" @click="onEditRole(row)" type="primary" size="mini"
              >编辑</span
            >
            <span class="operate-btn"
              type="primary"
              size="mini"
              @click="deleteMenu(row, $index)"
            >
              删除
            </span>
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="paginationOptions.pageNo"
          :page-sizes="paginationOptions.pageSizes"
          :page-size="paginationOptions.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="paginationOptions.total"
          size="mini"
        >
        </el-pagination>
      </div>
    </el-card>
    </div>
    <region-edit-dialog
      :toChild="list"
      ref="roleEditDialogRef"
    ></region-edit-dialog>
    <region-data-dialog
      :toChild="list"
      ref="roleDataDialogRef"
    ></region-data-dialog>
  </div>
</template>

<script>
// 真的地域接口
import { queryRegion, deletesRegion } from "@/api/region";

import regionEditDialog from "@/views/app/region/dialog/regionEdit.vue";
import regionDataDialog from "@/views/app/region/dialog/regionDetails.vue";
export default {
  components: {
    regionEditDialog,
    regionDataDialog,
  },
  data() {
    return {
      list: "",
      formOptions: {
        id: "",
        name: "",
        code: "",
      },
      tableData: [],
      paginationOptions: {
        pageNo: 1,
        pageSizes: [10, 20, 30, 50, 100],
        pageSize: 10,
        layout: "total, sizes, prev, pager, next, jumper",
      },
      // 验证
      rules: {
        name: [
          {
            required: false,
            message: "请输入地域名称",
            trigger: ["blur", "change"],
          },
          {
            pattern: /^[^\s]*$/,
            message: "不支持空格格式",
            trigger: "blur",
          },
          {
            pattern: /^([\u4E00-\u9FA5])*$/,
            message: "请输入中文名称",
            trigger: "blur",
          },
        ],
      },
    };
  },
  computed: {
    tableHeight() {
      return this.$store.state.tableHeight-440;
    }
  },
  mounted() {
    this.queryRoles();
  },
  methods: {
    // 手动 查询
    queryRolesclick() {
      this.$refs["queryRoleRef"].validate((valid) => {
        if (valid) {
          this.paginationOptions.pageNo = 1;
          this.queryRoles();
        } else {
          return false;
        }
      });
    },
    // 真的方法
    queryRoles() {
      this.$refs["queryRoleRef"].validate((valid) => {
        if (valid) {
          let data = { records: [{ ...this.formOptions }] };
          data.current = this.paginationOptions.pageNo;
          data.size = this.paginationOptions.pageSize;
          queryRegion(data).then((res) => {
            console.log(res, "res++++++++++");
            if (res && res.code && res.code === "00000") {
              // this.resetForm("queryRoleRef"); // 重置表单
              this.tableData = res.data.records; // 表格数据赋值
              this.paginationOptions.total = res.data.total; // 分页器赋值
              this.paginationOptions.pageNo=res.data.current
            }
          });
        } else {
          return false;
        }
      });
    },
    // 删除弹框
    deleteMenu(row, index) {
      this.$confirm("此操作将永久删除该地域, 是否继续?", "删除地域", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        cancelButtonClass: "btn-custom-cancel",
        type: "warning",
        modal: false,
      })
        .then(() => {
          // 点击确认，发起后台请求，删除该用户
          deletesRegion(row.id).then((res) => {
            console.log(res, "点击确认，发起后台请求，删除");
            if (res && res.code && res.code === "00000") {
              if(this.tableData.length==1&&this.paginationOptions.pageNo!=1){
                this.paginationOptions.pageNo--
              }
              this.queryRoles();
              return this.$message({
                type: "success",
                message: "删除成功!",
              });
            }
          });
        })
        .catch(() => {
          // 点击取消，取消该操作
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 查看
    lookClick(row) {
      this.$refs.roleDataDialogRef.openDialog(row);
      this.list = "查看地域详情";
      console.log("我要查看", row);
    },
    // 添加
    addClick() {
      this.$refs.roleEditDialogRef.openDialog();
      this.list = "添加地域";
      console.log("我要添加");
    },
    // 编辑
    onEditRole(row) {
      this.$refs.roleEditDialogRef.openDialog(row);
      this.list = "修改地域信息";
      console.log("编辑", row, row.id);
    },
    // 重置表单
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    // 分页器 页容量变更行为
    handleSizeChange(val) {
      this.paginationOptions.pageSize = val;
      this.queryRoles();
    },
    // 分页器 页码变更行为
    handleCurrentChange(val) {
      this.paginationOptions.pageNo = val;
      this.queryRoles();
    },
    indexMethod(index) {
      return (
        (this.paginationOptions.pageNo - 1) * this.paginationOptions.pageSize +
        index +
        1
      );
    },
  },
};
</script>

<style lang="less" scoped>
::v-deep .el-col-19 {
  text-align: right;
}
.el-form--inline .el-form-item {
  margin-right: 0;
}


.el-form-item {
  width: 252px;
}

</style>
